package net.onecloud.onecloud.account.resume;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.google.gson.Gson;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.model.ResultResume;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.model.Resume;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.PreferencesUtils;

import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class AddResumePage extends BaseAppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private EditText mRname;
    private RadioGroup mRsex;
    private EditText mRcontact;
    private EditText mRschool;
    private EditText mRposition;
    private EditText mRskill;
    private EditText mRexprience;
    private String sex = "女";

    Observer<ResultResume> addResumeObserver = new Observer<ResultResume>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ResultResume resultResume) {
            if (resultResume.getSuccess() == 1) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                List<ResultResume.DataEntity> list = resultResume.getData();
                if (list != null && list.size() > 0) {
                    bundle.putParcelable("resume", resultResume.getData().get(0));
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                }
                showHitMustFinish("添加成功");
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_resume_page);
        setToolBarTitle("添加简历");
        HideLeftButton(false);
        HideRightButton(false);
        initVies();
        initListener();
        initData();
    }

    private void initData() {
    }

    private void initListener() {
        mRsex.setOnCheckedChangeListener(this);
    }

    private void initVies() {
        mRname = (EditText) findViewById(R.id.m_rname);
        mRsex = (RadioGroup) findViewById(R.id.m_rsex);
        mRcontact = (EditText) findViewById(R.id.m_rcontact);
        mRschool = (EditText) findViewById(R.id.m_rschool);
        mRposition = (EditText) findViewById(R.id.m_rposition);
        mRskill = (EditText) findViewById(R.id.m_rskill);
        mRexprience = (EditText) findViewById(R.id.m_rexprience);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case 0: {
                sex = "女";
                break;
            }
            case 1: {
                sex = "男";
                break;
            }
        }

    }

    @Override
    public void onClickRightButton() {
        final Resume resume = getResume();
        if (resume != null) {
            Observable.just(App.getUser())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .flatMap(new Func1<ResultUser.DataEntity, Observable<ResultResume>>() {
                        @Override
                        public Observable<ResultResume> call(ResultUser.DataEntity dataEntity) {
                            if (dataEntity != null) {
                                resume.setUser_id(dataEntity.getId());
                                return NetWork.getResumeApi().add(resume).subscribeOn(Schedulers.io());
                            } else {
                                showHit("请先登录");
                                return null;
                            }

                        }
                    }).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(addResumeObserver);
        }


    }

    private Resume getResume() {
        String name = mRname.getText().toString();
        String contact = mRcontact.getText().toString();
        String school = mRschool.getText().toString();
        String position = mRposition.getText().toString();
        String skill = mRskill.getText().toString();
        String exprience = mRexprience.getText().toString();
        String info = "";
        if (TextUtils.isEmpty(name)) {
            info = "名字不能为空";
        } else if (TextUtils.isEmpty(contact)) {
            info = "联系方式不能为空";

        } else if (TextUtils.isEmpty(school)) {
            info = "学校不能为空";

        } else if (TextUtils.isEmpty(position)) {
            info = "岗位不能为空";

        } else if (TextUtils.isEmpty(skill)) {
            info = "技能不能为空";

        } else if (TextUtils.isEmpty(exprience)) {
            info = "经历不能为空";

        }
        if (!info.equals("")) {
            showHit(info);
            return null;
        }
        Resume resume = new Resume();
        resume.setRname(name);
        resume.setContact(contact);
        resume.setExprience(exprience);
        resume.setRposition(position);
        resume.setSchool(school);
        resume.setSex(sex);
        resume.setSkill(skill);
        return resume;
    }
}
